{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第二章：Python基础——变量和数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.1 变量-保存内容的地方\n",
    "  变量(variable)时我们希望在程序运行的时候计算机能够记住的内容。当Python“记住”某些内容时，它会将这些信息存储在计算机的内存中。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 方式一:不定义变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello Python World!\n",
      "Hello Python World!Hello Python World!\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello Python World!\")\n",
    "print(\"Hello Python World!Hello Python World!\") # 要求写两遍“Hello Python World”"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 如果该文字“Hello Python World”要求多次使用，显然复制粘贴的办法不够简洁的。为此，可以定义一个变量,以后使用到变量只需要调用它的名称，而不用管它的具体内容。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 方式二: 引入变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello Python World!\n",
      "Hello Python World!Hello Python World!\n"
     ]
    }
   ],
   "source": [
    "a=\"Hello Python World!\"\n",
    "print(a)\n",
    "print(2*a) #复制两遍"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 变量赋值：\n",
    "Python使用等号(=)给一个变量赋值。像x=6的赋值操作，告诉计算机记住数字6，并且当我们在任何时候\n",
    "使用x，都将6返回给我们。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 变量命名规则：\n",
    "* 变量名只能包含字母、数字与下划线。但不能以数字开头，如 合法变量student_1, 错误命名：1_student。\n",
    "* 变量名不能包括空格。\n",
    "* 不能与Python内置函数重名。\n",
    "* 变量名应既简短又具有描述性。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 变量数据类型：\n",
    "包括数值、字符串与布尔型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0\n",
      "3.0\n"
     ]
    }
   ],
   "source": [
    "# 数值型变量\n",
    "a=1  # 整数型\n",
    "b=2\n",
    "c=2.0  #浮点型\n",
    "print(b/a)\n",
    "print(a+c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am a student at NAU. I'm a student at NAU\n"
     ]
    }
   ],
   "source": [
    "# 字符串型变量\n",
    "str_1=\"I am a student at NAU.\" # 字符串可以用两个单引号或双引号来定义。\n",
    "str_2=\"I'm a student at NAU\"  # 如果字符串里面出现单引号“‘”，两端必须用双引号。\n",
    "print(str_1,str_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 布尔型变量，当条件为真，输出值为True;否则为False\n",
    "d=2>1 \n",
    "print(d)\n",
    "e=1>3\n",
    "print(e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 习题练习： 以$\\pi$为例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C=31.4159  S=78.53975  V=523.5983333333332\n"
     ]
    }
   ],
   "source": [
    "pi=3.14159\n",
    "r=5\n",
    "C=2*pi*r # 圆的周长\n",
    "S=r**2*pi # 圆的面积\n",
    "V=r**3*pi*4/3   #圆形体积\n",
    "print(\"C=\"+str(C),\" S=\"+str(S),\" V=\"+str(V))  # 字符串的“+”表示连接。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello Python World!Hello Python World!\n"
     ]
    }
   ],
   "source": [
    "print(a*2)  # 输出变量a两次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Xiao Ming loves Python and SQL.\n"
     ]
    }
   ],
   "source": [
    "a = \"loves Python and SQL.\" # 重新给a赋值，则旧的值会被掩盖\n",
    "b=\"Xiao Ming\"\n",
    "print(b+\" \"+a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.2 Python数据类型查找与运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#整数(int)、浮点数(float)、字符串(charater)、布尔值(bool)和空值(None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(9) # type是用来输出数据类型的内置函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(3.14)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(\"Hello\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 整型运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2*3 # 星号 “*‘”表示乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.0"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "6/1.5 # 斜杠 “/”表示除法，输出数据类型为浮点数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2**4 # 双星号 “**”表示乘方"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10%3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 浮点型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.5"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3/2 # 斜杠 “/”表示除法，输出数据类型为浮点数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.3 字符串操作\n",
    "包括截取(分片)、连接、分割、删除两端字符、格式化操作等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python\n"
     ]
    }
   ],
   "source": [
    "str1=\"Python\"\n",
    "print(str1) # 输出字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  截取操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pytho\n"
     ]
    }
   ],
   "source": [
    "print(str1[0:-1]) # 输出第一个到倒数第二个的所有字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P\n"
     ]
    }
   ],
   "source": [
    "print(str1[0]) # 输出字符串第一个字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "th\n"
     ]
    }
   ],
   "source": [
    "print(str1[2:4]) # 输出从第三个开始到第四个的字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "thon\n"
     ]
    }
   ],
   "source": [
    "print(str1[2:]) # 输出从第三个开始的后的所有字符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 连接操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PythonPython\n",
      "PythonLearning\n",
      "Python3.7\n"
     ]
    }
   ],
   "source": [
    "print(str1 * 2) # 输出字符串两次\n",
    "print(str1 + \"Learning\") # 连接字符串\n",
    "print(str1+str(3.7)) # 必须把数值类型转换为字符串类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 分割操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['www', 'baidu', 'com']"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str3='www.baidu.com'\n",
    "str3.split('.') # 括号内指定分割符号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['www', 'baidu', 'com']"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str3=\"www.baidu.com\"\n",
    "str3.split('.') # 括号内指定分割符号"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 删除字符串两端符号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "www.nau.edu.cn\n",
      "www.nau.edu.cn \n",
      " www.nau.edu.cn\n",
      "www.nau.edu.cn\n"
     ]
    }
   ],
   "source": [
    "str4=\" www.nau.edu.cn \"\n",
    "print(str4.strip()) # 删除两端空格符\n",
    "print(str4.lstrip()) #删除左端空格符\n",
    "print(str4.rstrip()) # 删除右端空格符\n",
    "str5=\"$#www.nau.edu.cn@*&\"\n",
    "print(str5.strip(\"#$@*&\")) #把不想要字符串两端的符号写入strip()的小括号内。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 格式化操作\n",
    "当字符串需要插入来自外部的变量时，这时需要使用格式化操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我的名字是:小明, 年龄为20\n"
     ]
    }
   ],
   "source": [
    "name=\"小明\"\n",
    "age=20\n",
    "print(\"我的名字是:{}, 年龄为{}\".format(name,age)) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 布尔型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1>2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1>2 and 2>1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1>2 or 2>1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "a = 1>2 or 2>1\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "print(not a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 空值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 空值与0不同"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
